package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import csc.daonjpa.java.controller.EntityManagerFactoryUtil;
import csc.daonjpa.java.domain.TargetAccount;

@Component
public class TargetAccountDAO {
	public List<TargetAccount> getTargetAccountList(){
		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil.createEntityManager();
		
		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();
		
		// -----------Begin transaction-----------
		List<TargetAccount> targetAccounts = null;
		try {
			entr.begin();
			// Get a list of accounts from DB
			
			TypedQuery<TargetAccount> query = entityManager.createQuery("SELECT ta FROM " + TargetAccount.class.getName() + " ta", TargetAccount.class);
			targetAccounts = query.getResultList();
			
			entr.commit();
		} catch (Exception e) {
			entityManager.close();
		}
		// -----------End transaction-----------

		return targetAccounts;
	}
}
